﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script>
    var userid = $.request("userid");
    var userList = [], itemList = [];
    $(function () {
        $("#state_horizon a.btn-default").click(function () {
            $("#state_horizon a.btn-default").removeClass("active");
            $(this).addClass("active");
            $('#btn_search').trigger("click");
        });
        getItemList();
        getUserList();
        getDistrict(0);
        initControl(itemList);
        gridList(itemList);
    })
    function gridList(ItemList) {
        var $gridList = $("#gridList");
        var isp = $("#state_horizon a.active").attr('data-value');
        var queryJson = {
            IsProcessed: isp,
            userid: userid
        }
        $gridList.dataGrid({
            url: "/SiteManage/SiteDemand/GetGridJson",
            postData: { queryJson: JSON.stringify(queryJson) },
            height: $(window).height() - 96,
            colModel: [
                {
                    label: '省份', name: 'F_ProvincesId', width: 150, align: 'left', formatter: function (cellvalue) {
                        return GetListByFId(cellvalue);
                    }
                },
                {
                    label: '城市', name: 'F_CityId', width: 150, align: 'left', formatter: function (cellvalue) {
                        return GetListByFId(cellvalue);
                    }
                },
                {
                    label: '区域', name: 'F_DistrictId', width: 150, align: 'left', formatter: function (cellvalue) {
                        return GetListByFId(cellvalue);
                    }
                },
                {
                    label: '商圈', name: 'F_BusinessArea', width: 150, align: 'left', formatter: function (cellvalue) {
                        return GetListByFId(cellvalue);
                    }
                },
                {
                    label: '场地类型', name: 'F_SiteType', width: 100, align: 'left', formatter: function (cellvalue) {
                        return FMDictionaries(ItemList.SiteType, cellvalue);
                    }
                },
                { label: '容纳人数', name: 'F_MaxPeople', width: 100, align: 'left' },
                { label: '面积', name: 'F_Area', width: 100, align: 'left' },
                { label: '层高', name: 'F_Height', width: 100, align: 'left' },
                {
                    label: '申请人', name: 'F_CreatorUserId', width: 100, align: 'left', formatter: function (cellvalue) {
                        if (userList != null) {
                            for (var i = 0; i < userList.length; i++) {
                                if (userList[i].F_Id == cellvalue) {
                                    cellvalue = userList[i].F_NickName;
                                    break;
                                } else {
                                    continue;
                                }
                            }
                        }
                        return cellvalue == 'null' ? "" : cellvalue;
                    }
                },
                {
                    label: '操作', name: 'F_Id', width: 120, align: 'left', sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        var html = '<div class="btn-group btn-group-xs" style="margin-top:5px;">';
                        // 状态为待处理并且当前为BD人员
                        if (isp == "0" && userid == "1") {
                            html += '<button type="button" class="btn btn-link" onclick="btn_UpdateState(\'' + cellvalue + '\')">已处理</button>';
                        }
                        // 状态为待处理并且当前为需求提出人
                        if (isp == "0" && userid =="0") {
                            html += '<button type="button" class="btn btn-link" onclick="btn_delete(\'' + cellvalue + '\')">删除</button>';
                        }
                        html += '</div>';

                        return html;
                    }
                },
                { label: '', name: 'F_Id', hidden: true }
            ],
            pager: "#gridPager",
            sortname: 'F_Id desc',
            viewrecords: true,
            ondblClickRow: function (rowid, iRow, iCol, e) {
                // 状态为待处理并且当前为需求提出人
                if (isp == "0" && userid == "0") {
                    var rowobj = $gridList.getRowData(rowid);
                    $.modalOpen({
                        id: rowobj["F_Id"],
                        title: "修改场地需求",
                        url: "/SiteManage/SiteDemand/Form?keyValue=" + rowobj["F_Id"],
                        width: "700px",
                        height: "520px",
                        callBack: function (iframeId) {
                            top.frames[iframeId].submitForm();
                        }
                    });
                }
            }
        });
        $("#btn_search").click(function () {
            isp = $("#state_horizon a.active").attr('data-value');
            var queryJsons = {
                IsProcessed: isp,
                SiteType: $("#site_type").val(),
                ProvincesId: $("#provinces_id").val(),
                city: $("#city_id").val(),
                DistrictId: $("#district_id").val(),
                BusinessArea: $("#business_area").val(),
                userid: userid
            }
            $gridList.jqGrid('setGridParam', {
                postData: { queryJson: JSON.stringify(queryJsons) },
            }).trigger('reloadGrid');
        });
        $(".ui-jqgrid,.ui-jqgrid-view,.ui-jqgrid-htable,.ui-jqgrid-hdiv,.ui-jqgrid-bdiv,.ui-jqgrid-pager").css("width", function () { return "100%"; });
        $(".ui-jqgrid-btable").css("width", function () { return "99%"; });
    }

    function getUserList() {
        $.ajax({
            url: "/SystemManage/User/GetUsersJson",
            dataType: "json",
            async: false,
            success: function (data) {
                userList = data;
            }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
    }
    //转换数据字典数据
    function FMDictionaries(state, cellvalue) {
        if (cellvalue != undefined && cellvalue != null) {
            for (var i = 0; i < state.length; i++) {
                if (state[i].F_ItemCode.toString() == cellvalue.toString()) {
                    cellvalue = state[i].F_ItemName;
                    break;
                }
            }
        }
        else
            cellvalue = "";
        return cellvalue;
    }
    //转换省市区商圈数据
    function GetListByFId(cellvalue) {
        if (cellvalue != undefined && cellvalue != null) {
            $.ajax({
                url: "/SystemManage/Area/GetListByFId?code=" + cellvalue,
                dataType: "json",
                async: false,
                success: function (data) {
                    if (data.length > 0) {
                        cellvalue = data[0].F_FullName;
                    }
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
        }
        else
            cellvalue = "";
        return cellvalue;
    }

    function getItemList() {
        itemList = getCache("SJZD");
        if (!itemList || itemList.length <= 0) {
            $.ajax({
                url: "/SiteManage/Site/GetItemListJson",
                dataType: "json",
                async: false,
                success: function (data) {
                    itemList = data;
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
        }
    }

    function btn_add() {
        $.modalOpen({
            id: "SiteDemand",
            title: "修改场地需求",
            url: "/SiteManage/SiteDemand/Form?keyValue=SiteDemand",
            width: "700px",
            height: "520px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_edit() {
        var keyValue = $("#gridList").jqGridRowValue().F_Id;
        $.modalOpen({
            id: "Form",
            title: "修改场地需求",
            url: "/SiteManage/SiteDemand/Form?keyValue=" + keyValue,
            width: "700px",
            height: "520px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_UpdateState(value) {
        $.submitForm({
            url: "/SiteManage/SiteDemand/UpdateState",
            param: { keyValue: value },
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        })
    }

    function btn_delete(value) {
        $.deleteForm({
            url: "/SiteManage/SiteDemand/DeleteForm",
            param: { keyValue: value },
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        })
    }
    function getDistrict(type) {
        var pidValue, actionid;
        if (type == "city") {
            pidValue = $("#provinces_id").val();
            actionid = "city_id";
        }
        else if (type == 'district') {
            pidValue = $("#city_id").val()
            actionid = "district_id";
        }
        else if (type == 'business') {
            pidValue = $("#district_id").val();
            actionid = "business_area";
        }
        else {
            pidValue = 0;
            actionid = "provinces_id";
            $("#provinces_id").append("<option value='' class='select2-selection__placeholder'>==请选择省份==</option>");
        }
        $.ajax({
            url: "/SystemManage/Area/GetListByParentId?code=" + pidValue,
            dataType: "json",
            async: false,
            success: function (data) {
                CityControl(data, type, actionid);
            }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
    }
    function CityControl(ItemList, type, actionid) {
        var items = ItemList;
        if (type == "district") {
            var bindid = $("#district_id,#business_area");
            bindid.empty();
            $("#district_id").append("<option value='' selected='selected'>==请选择行政区域==</option>");
            $("#business_area").append("<option value='' selected='selected'>==请选择商圈==</option>");
            bindid.bindSelect();
            bindid.select2();
        } else if (type == "city") {
            var bindid = $("#city_id,#district_id,#business_area");
            bindid.empty();
            $("#city_id").append("<option value='' selected='selected'>==请选择城市数据==</option>");
            $("#district_id").append("<option value='' selected='selected'>==请选择行政区域==</option>");
            $("#business_area").append("<option value='' selected='selected'>==请选择商圈==</option>");
            bindid.bindSelect();
            bindid.select2();
        }
        else if (type == "business") {
            var bindid = $("#business_area");
            bindid.empty();
            $("#business_area").append("<option value='' selected='selected'>==请选择商圈==</option>");
            bindid.bindSelect();
            bindid.select2();
        }
        for (var i = 0; i < items.length; i++) {
            $("#" + actionid).append("<option value='" + items[i].F_Id + "'>" + items[i].F_FullName + "</option>");
        }
        $("#" + actionid).bindSelect()//行政区域\商圈
        $("#" + actionid).select2();
    }
    function initControl(ItemList) {
        var sitetype = ItemList.SiteType;
        $("#site_type").append("<option value='' class='select2-selection__placeholder'>==请选择场地类型==</option>");
        for (var i = 0; i < sitetype.length; i++) {
            $("#site_type").append("<option value='" + sitetype[i].F_ItemCode + "'>" + sitetype[i].F_ItemName + "</option>");
        }

        $("#city_id,#district_id,#site_type,#business_area,#through_three_areas").bindSelect()//行政区域
        $("#business_area").append("<option value='' class='select2-selection__placeholder'>==请选择商圈==</option>");
        $("#district_id").append("<option value='' class='select2-selection__placeholder'>==请选择行政区域==</option>");
        $("#city_id").append("<option value='' class='select2-selection__placeholder'>==请选择城市==</option>");
        $('#city_id,#district_id,#provinces_id,#site_type,#business_area,#through_three_areas').select2();
    }
</script>

<div  style="height:100%;">
    <div class="toolbar" style="width:25%;float:right; text-align:right;">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
        </div>
        <div class="btn-group">
            <a id="NF-add" authorize="yes" class="btn btn-primary dropdown-text" onclick="btn_add()"><i class="fa fa-eraser"></i>添加按钮</a>
        </div>
        <div class="operate">
            <ul class="nav nav-pills">
                <li><a id="NF-edit" authorize="yes" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>修改按钮</a></li>
                <li><a id="NF-delete" authorize="yes" onclick="btn_delete()"><i class="fa fa-trash-o"></i>删除按钮</a></li>
            </ul>
            <a href="javascript:;" class="close"></a>
        </div>
        <script>$('.toolbar').authorizeButton()</script>
    </div>

    <div style="width:75%;">
        <table class="form">
            <tr>
                <td class="formValue" style="margin: 0 10px;">
                    <select id="site_type" name="site_type" class="form-control"></select>
                </td>
                <td class="formValue">
                    <select id="provinces_id" onchange="getDistrict('city')" class="form-control"></select>
                </td>
                <td class="formValue">
                    <select id="city_id" onchange="getDistrict('district')" name="city_id" class="form-control"></select>
                </td>
                <td class="formValue">
                    <select id="district_id" onchange="getDistrict('business')" name="district_id" class="form-control"></select>
                </td>
                <td class="formValue">
                    <select id="business_area" name="business_area" class="form-control"></select>
                </td>
            </tr>
            <tr>
                <td class="formValue" style="padding-left: 10px;" colspan="4">
                    <div id="state_horizon" class="btn-group">
                        <a class="btn btn-default active" data-value="0">待处理</a>
                        <a class="btn btn-default" data-value="1">已处理</a>
                    </div>
                </td>
                <td class="formValue" style="text-align:right">
                    <div class="input-group">
                        <button id="btn_search" type="button" class="btn  btn-primary" style="padding:0 20px">按条件筛选</button>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>

<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>


